<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>标题</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/lib/layui/css/layui.css">
    <link rel="stylesheet" href="/statics/layui/font/font.css" media="all"/>
    <link rel="stylesheet" href="/static/css/weadmin.css">
    <style type="text/css">
        html, body {
            height: 100%;
        }

        .chooseBox {
            border: 1px solid #333;
            height: 300px;
            padding: 10px;
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
            overflow-y: auto;
        }

        .leftBox > p {
            line-height: 30px;
            padding-left: 20px;
            cursor: pointer;
        }

        .leftBox > p:hover {
            background: #ddd;
        }

        .leftBox > p.selected {
            background: #d5d5d5;
        }
    </style>
</head>
<body>
<div class="layui-fluid" style="height:100%;">
    <form class="layui-form" lay-filter="formData" style="margin-top: 10px;">
        <input type="hidden" name="userId" class="formData">
        <div class="layui-form-item">
            <label class="layui-form-label required">工区名称：</label>
            <div class="layui-input-block">
                <input type="hidden" name="areaId" class="formData">
                <input type="hidden" name="prjId" class="formData">
                <input type="hidden" name="parentId" class="formData">
                <input type="text" name="areaName" lay-verify="required" placeholder="工区名称" autocomplete="off"
                       class="layui-input formData"/>
            </div>
        </div>
        <div class="layui-inline" id="responsibleUserDiv" style="display:none">
            <label class="layui-form-label required">负责人：</label>
            <div class="layui-input-block" style="width: 100%;">
                <select id="responsibleUserId" name="responsibleUserId" lay-verify="required" lay-search="" class="formData">
                    <option value="">直接选择或搜索选择</option>
                </select>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript" src="/lib/layui/layui.js"></script>
<script src="/static/js/main.js"></script>
<!--<script type="text/javascript" src="../../../statics/js/sys/editDict.js"  charset="utf-8"></script>-->
<script>
    layui.use(['form', 'sessionFun', 'configure', 'configureCom', 'ztree'], function () {
        window.$ = layui.$;
        var form = layui.form;
        var configure = layui.configure;
        ztree = layui.ztree;
        var configureCom = layui.configureCom;
        var sessionFun = layui.sessionFun;
        var listData = sessionFun.get(configure.SESSION_LIST_DATA);
        var saveSubWorkArea = configureCom.GetQueryString('saveSubWorkArea');
        var token = sessionFun.get(configure.SESSION_TOKEN) ? sessionFun.get(configure.SESSION_TOKEN).token : '';

        // 有工区负责人时 赋值 responsibleUserId
        if(listData && listData.responsibleUserId && (saveSubWorkArea==0) ){
            $("#responsibleUserId").append('<option value="'+listData.responsibleUserId+'">'+listData.responsibleUserName+'</option>');
        }

        //子工区, 不显示工区负责人
        if(saveSubWorkArea==1){
            //操作的是子工区,并且是新建子工区(点击一级工区进来)
            if(listData.parentId==0){
                //后面逻辑会用listData初始化表单值, 当新建子工区时, 给parentId赋值
                listData.parentId=listData.areaId;
                listData.areaId='';
                listData.areaName='';
            }
        }else{
            $("#responsibleUserDiv").show();
        }

        //查找本项目(或机构)下所有人员
        $.ajax({
            type: "POST",
            url: configure.COM_USER_YEWU_LIST,
            data: {"token": token},
            success: function (data) {
                var code = data.code;
                var msg = data.msg;
                if (code == "0" || msg == "success") {
//                    $("#responsibleUserId").empty();
//                    $("#responsibleUserId").append('<option value="">'+ '直接选择或搜索选择</option>');
                    var list = data.page.records;
                    for (var idx in list) {
                        if(listData && listData.responsibleUserId == list[idx].userId){
                        }else{
                            $("#responsibleUserId").append('<option value="'+list[idx].userId+'">'+ list[idx].realName +'</option>');
                        }
                    }
                    //重新渲染form表单  否则动态option不生效
                    form.render();
                } else {
                    layer.msg(data.msg);
                }
                layer.closeAll('loading');
            }
        });

        if (listData) {
            // var roleNameList = '';
            // var roleIdList = '';
            // $(listData.roleList).each(function (index, item) {
            //     if (!index) {
            //         roleIdList = item.roleId;
            //         roleNameList = item.roleName;
            //     } else {
            //         roleIdList += ',' + item.roleId;
            //         roleNameList += ',' + item.roleName;
            //     }
            // });
            // var orgName = '';
            // var orgNoList = '';
            // $(listData.orgList).each(function (index, item) {
            //     if (!index) {
            //         orgNoList = item.orgNo;
            //         orgName = item.orgName;
            //     } else {
            //         orgNoList += ',' + item.orgNo;
            //         orgName += ',' + item.orgName;
            //     }
            // });
            // listData.orgNoList = orgNoList;
            // listData.orgName = orgName;
            //
            // listData.roleIdList = roleIdList;
            // listData.roleNameList = roleNameList;
            // $("#roleIdList").val(roleIdList);
        }
        console.log(listData);
        // 赋值
        form.val('formData', listData);
        //
        form.render();
        // $('.layui-form').on('click', '#orgName', function () {
        //     var $this = $(this);
        //     var value = $(this).next().val();
        //     top.layer.open({
        //         type: 2,
        //         title: "选择单位",
        //         area: configureCom.Psize1,
        //         content: configureCom.nocache(configure.USER_COMPANY_HTML),
        //         btn: ['选择', '关闭'],
        //         yes: function (index, layero) {
        //             var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对
        //             var orgNameData = "";
        //             var orgNoData = "";
        //             var data = iframeWin.getData();
        //             $(data).each(function (index, item) {
        //                 if (!index) {
        //                     orgNameData += item.orgName;
        //                     orgNoData += item.orgNo;
        //                 } else {
        //                     orgNameData += "," + item.orgName;
        //                     orgNoData += "," + item.orgNo;
        //                 }
        //             });
        //             $this.val(orgNameData);
        //             $this.next().val(orgNoData);
        //             top.layer.close(index);
        //         }
        //     })
        // });
        // $('.layui-form').on('click', '#roleNameList', function () {
        //     var $this = $(this);
        //     var value = $(this).next().val();
        //     top.layer.open({
        //         type: 2,
        //         title: "选择角色",
        //         area: configureCom.Psize1,
        //         content: configureCom.nocache(configure.USER_ROLE_HTML),
        //         btn: ['选择'],
        //         yes: function (index, layero) {
        //             var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对
        //             var data = iframeWin.getData();
        //             var roleIdData = '';
        //             var roleNameData = '';
        //             $(data).each(function (index, item) {
        //                 if (!index) {
        //                     roleIdData += item.roleId;
        //                     roleNameData += item.roleName;
        //                 } else {
        //                     roleIdData += ',' + item.roleId;
        //                     roleNameData += ',' + item.roleName;
        //                 }
        //             });
        //             $this.val(roleNameData);
        //             $this.next().val(roleIdData);
        //             top.layer.close(index);
        //         }
        //     });
        // });
    });

    function getData() {
        var data = {};
        $('.formData').each(function (index, item) {
            var key = $(item).attr('name');

            data[key] = $(item).val();
        });
        return data;
    }
</script>
</body>
</html>
